//
//  BaseController.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/9.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

class BaseController: UITableViewController {
    
    lazy var visitorView : VisitorView = VisitorView.visitorView()
    
    var isLogin : Bool = PersonViewModel.shareIntance.isLogin
//    var isLogin : Bool = false
    
    
    // MARK:- 系统回调函数
    override func loadView() {
        
        //判断是否登录
        isLogin ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationItems()
        
    }

}

// MARK:- 未登陆加载视图
extension BaseController{
    fileprivate func setupVisitorView() {
        
        view = visitorView
        
        visitorView.registorBtn.addTarget(self, action: #selector(registerBtnClick), for: .touchUpInside)
        
        visitorView.loginBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
    }
    
    /// 设置导航栏左右的item
    fileprivate func setupNavigationItems(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "注册", style: .plain, target: self, action: #selector(registerBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "登录", style: .plain, target: self, action: #selector(loginBtnClick))
        
    }
     
}

// MARK:- 事件监听
extension BaseController{
    
    @objc fileprivate func registerBtnClick(){
        
        NetworkTools.shareInstance.request(methodTyle: .post, urlString: "http://httpbin.org/post", parameters: ["origin" : "24.127.96.129" as AnyObject]) { (result :AnyObject?, error : Error?) in
            
            if error != nil{
                print(error)
                return
            }
            
            print(result)
            
        }
    }
    
    @objc fileprivate func loginBtnClick(){
    
        let oauthVc = OauthController()
        let oauthNav = UINavigationController.init(rootViewController: oauthVc)
        
        present(oauthNav, animated: true, completion: nil)
    }
}









